var $TOPICID = "";
window.onload = function(){
	$POSMENU=3
	new CHECKLOGIN().check();
	new TOPIC().list();
	document.myform.reset();
}
function preSubmit(action){
	var title = $N("title")[0].value;
	if(title=="") return;
	$N("action")[0].value = action;
	document.myform.submit();
}
function onAddTopic(value,msg){
	if(value=="true"){
		alert("Done");
		new TOPIC().list();
	}else{
		alert("Error");
	}
}
TOPIC=function(){}
TOPIC.prototype.list=function(){
	var fncSuc = XHR.Delegate(this,this.listSuccess);
	var param = {action:"listTopic"};
	new XHR("post",fncSuc).send($PN+"/DoAdmin",param);
}
TOPIC.prototype.listSuccess=function(r)	{
	var xml = r.xml;
	if(getRV(xml)){
		var html = "";
		var il = xml.getElementsByTagName("topic");
		for(var i=0; i < il.length; i++){
			html+="<div style='padding:3px'><table style='cursor:pointer'><tr><td width='55px'>"+
			"<img width='50px'  onClick='detailTopic(\""+il[i].getAttribute("id")+"\")' valign='top' src='"+genLinkImg(il[i].getAttribute("id"),il[i].getAttribute("thumbnail"))+"'>"+
			"</td><td valign='top'><span style='cursor:pointer'  onClick='detailTopic(\""+il[i].getAttribute("id")+"\")'><b>"+il[i].getAttribute("name")+"</b></span>" +
			"<div style='color:#999'>"+fulldateSQLtoN(il[i].getAttribute("date"))+"</div>"+
			"<div style='color:red;font-size:12px;padding:3px 0px 3px 0px;cursor:pointer' onClick='deleteTopic(\""+il[i].getAttribute("id")+"\")'>[Xóa]</div>"+
			"</td></tr</table></div>";
		}
		$("topiclist").innerHTML = html;
	}else{
		$("topiclist").innerHTML = "<b>Lỗi rồi</b>";
	}
}
TOPIC.prototype.detail=function(topicId){
	var fncSuc = XHR.Delegate(this,this.detailSuccess);
	var param = {action:"detailTopic",topicId:topicId};
	new XHR("post",fncSuc).send($PN+"/DoAdmin",param);
}
TOPIC.prototype.detailSuccess=function(r)	{
	var xml = r.xml;
	if(getRV(xml)){
		var html = "";
		var il = xml.getElementsByTagName("topic")[0];
		$N("title")[0].value = il.getAttribute("name");
		$N("topicId")[0].value = il.getAttribute("id");
		$N("createdDate")[0].value = fulldateSQLtoN(il.getAttribute("date"));
		$N("thumbnail")[0].value = il.getAttribute("thumbnail");
		$N("catId")[0].value = il.getAttribute("catid");
	}else{
		
	}
}

TOPIC.prototype.listNews=function(topicId){
	var fncSuc = XHR.Delegate(this,this.listNewsSuccess);
	var param = {action:"listNewsInTopic",topicId:topicId};
	new XHR("post",fncSuc).send($PN+"/DoAdmin",param);
}
TOPIC.prototype.listNewsSuccess=function(r)	{
	var xml = r.xml;
	if(getRV(xml)){
		var html = "";
		var il = xml.getElementsByTagName("news");
		for(var i=0; i < il.length; i++){
			html+="<div style='padding:3px'><span style='cursor:pointer;padding-right:5px' onClick='deleteNewsTopic(\""+il[i].getAttribute("newsid")+"\")'>[Xóa]</span>"+
			il[i].getAttribute("title")+"</div>";
		}
		if(html=="") html = "<b>Chưa có tin</b>";
		$("newslist").innerHTML = html;
	}else{
		$("newslist").innerHTML = "<b>Lỗi rồi</b>";		
	}
}

TOPIC.prototype.addNews=function(topicId,newsId){
	var fncSuc = XHR.Delegate(this,this.addNewsSuccess);
	var param = {action:"addNewsToTopic",topicId:topicId,newsId:newsId};
	new XHR("post",fncSuc).send($PN+"/DoAdmin",param);
	this.topicId = topicId;
}
TOPIC.prototype.addNewsSuccess=function(r)	{
	var xml = r.xml;
	if(getRV(xml)){
		detailTopic(this.topicId);
	}else{
		
	}
}

TOPIC.prototype.deleteTopic=function(topicId){
	var fncSuc = XHR.Delegate(this,this.deleteTopicSuccess);
	var param = {action:"updateStatusTopic",topicId:topicId,Status:-1};
	new XHR("post",fncSuc).send($PN+"/DoAdmin",param);
	this.topicId = topicId;
}
TOPIC.prototype.deleteTopicSuccess=function(r)	{
	var xml = r.xml;
	if(getRV(xml)){
		this.list();
	}else{
		
	}
}


TOPIC.prototype.deleteNews=function(topicId,newsId){
	var fncSuc = XHR.Delegate(this,this.addNewsSuccess);
	var param = {action:"deleteNewsTopic",topicId:topicId,newsId:newsId};
	new XHR("post",fncSuc).send($PN+"/DoAdmin",param);
	this.topicId = topicId;
}

function detailTopic(id){
	new TOPIC().detail(id);
	new TOPIC().listNews(id);
	$TOPICID = id;
}
function addNewsToTopic(){
	var param = $N("link")[0].value;
	var newsid = param.split("/")[5];
	new TOPIC().addNews($TOPICID,newsid);
}
function deleteNewsTopic(newsid){
	if(confirm("Bạn có muốn xóa?")){
		new TOPIC().deleteNews($TOPICID,newsid);
	}
}
function deleteTopic(topicId){
	if(confirm("Bạn có muốn xóa? Cẩn thận khi làm hành động này")){
		new TOPIC().deleteTopic(topicId);
	}
}